<html>
<head><meta charset="utf-8"><title>Help with attribute macro basics · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Help.20with.20attribute.20macro.20basics.html">Help with attribute macro basics</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="182195597"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Help%20with%20attribute%20macro%20basics/near/182195597" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BatmanAoD (Kyle Strand) <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Help.20with.20attribute.20macro.20basics.html#182195597">(Nov 29 2019 at 18:08)</a>:</h4>
<p>I'm trying to write a simple attribute-like macro for the first time and am having trouble. Would anyone be willing to walk through some bits with me?</p>
<p>For instance, the macro is defined in <code>src/lib.rs</code>, and I'm trying to write a test in <code>tests/main.rs</code>, and it seems the macro must be imported using the name of the crate rather than the <code>crate</code> keyword. I seem to be running in to a lot of confusing problems like this.</p>



<a name="182195830"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Help%20with%20attribute%20macro%20basics/near/182195830" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BatmanAoD (Kyle Strand) <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Help.20with.20attribute.20macro.20basics.html#182195830">(Nov 29 2019 at 18:12)</a>:</h4>
<p>If anyone would like to take a look, the code is here: <a href="https://github.com/BatmanAoD/flexible_bin_heap" target="_blank" title="https://github.com/BatmanAoD/flexible_bin_heap">https://github.com/BatmanAoD/flexible_bin_heap</a></p>



<a name="182196548"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Help%20with%20attribute%20macro%20basics/near/182196548" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BatmanAoD (Kyle Strand) <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Help.20with.20attribute.20macro.20basics.html#182196548">(Nov 29 2019 at 18:28)</a>:</h4>
<p>The error I'm currently seeing that I don't understand is:</p>
<div class="codehilite"><pre><span></span>error[E0214]: parenthesized type parameters may only be used with a `Fn` trait
  --&gt; tests/main.rs:26:1
   |
26 | #[with_comparator(data2)]
   | ^^^^^^^^^^^^^^^^^^^^^^^^^
   | |
   | only `Fn` traits may use parentheses
   | help: use angle brackets instead: `#[with_comparator&lt;data2)&gt;`
</pre></div>


<p>The unmatched parenthesis in the <code>help</code> message is interesting but not enlightening. Comparing my macro against those in some other crates that use parentheses, I don't see the difference.</p>



<a name="182197644"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Help%20with%20attribute%20macro%20basics/near/182197644" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Help.20with.20attribute.20macro.20basics.html#182197644">(Nov 29 2019 at 18:51)</a>:</h4>
<p><a href="https://github.com/BatmanAoD/flexible_bin_heap/blob/cf1bfe68ea8d31b2ee9d548f0b83f181fa0ad142/src/lib.rs#L53" target="_blank" title="https://github.com/BatmanAoD/flexible_bin_heap/blob/cf1bfe68ea8d31b2ee9d548f0b83f181fa0ad142/src/lib.rs#L53">https://github.com/BatmanAoD/flexible_bin_heap/blob/cf1bfe68ea8d31b2ee9d548f0b83f181fa0ad142/src/lib.rs#L53</a></p>
<p>That is true for derive macros, but not for attribute macros</p>



<a name="182197708"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Help%20with%20attribute%20macro%20basics/near/182197708" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Help.20with.20attribute.20macro.20basics.html#182197708">(Nov 29 2019 at 18:52)</a>:</h4>
<p><a href="https://github.com/BatmanAoD/flexible_bin_heap/blob/cf1bfe68ea8d31b2ee9d548f0b83f181fa0ad142/src/lib.rs#L56" target="_blank" title="https://github.com/BatmanAoD/flexible_bin_heap/blob/cf1bfe68ea8d31b2ee9d548f0b83f181fa0ad142/src/lib.rs#L56">https://github.com/BatmanAoD/flexible_bin_heap/blob/cf1bfe68ea8d31b2ee9d548f0b83f181fa0ad142/src/lib.rs#L56</a></p>
<p>You accidentially used <code>Option()</code> instead of <code>Option&lt;&gt;</code>.</p>



<a name="182204131"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Help%20with%20attribute%20macro%20basics/near/182204131" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BatmanAoD (Kyle Strand) <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Help.20with.20attribute.20macro.20basics.html#182204131">(Nov 29 2019 at 21:05)</a>:</h4>
<blockquote>
<p>That is true for derive macros, but not for attribute macros</p>
</blockquote>
<p>Thanks! I see the bit I missed in the reference now -- "The returned <code>TokenStream</code> <em>replaces</em> the item..."</p>



<a name="182208381"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Help%20with%20attribute%20macro%20basics/near/182208381" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BatmanAoD (Kyle Strand) <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Help.20with.20attribute.20macro.20basics.html#182208381">(Nov 29 2019 at 22:36)</a>:</h4>
<blockquote>
<p>You accidentially used <code>Option()</code> instead of <code>Option&lt;&gt;</code>.</p>
</blockquote>
<p>Ahh, okay, that fiexes the "parenthesized type parameters" error; thank you! I thought that was referring to the parentheses around <code>data2</code>.</p>



<a name="182208713"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Help%20with%20attribute%20macro%20basics/near/182208713" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Help.20with.20attribute.20macro.20basics.html#182208713">(Nov 29 2019 at 22:45)</a>:</h4>
<p>These suggestions are just heuristics which often go wrong with macros</p>



<a name="182209662"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Help%20with%20attribute%20macro%20basics/near/182209662" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BatmanAoD (Kyle Strand) <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Help.20with.20attribute.20macro.20basics.html#182209662">(Nov 29 2019 at 23:06)</a>:</h4>
<p><span class="user-mention" data-user-id="133247">@bjorn3</span> That also gave me enough of an understanding of what it "looks like" when things go wrong to get it working entirely. Thanks again!</p>



<a name="182209696"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Help%20with%20attribute%20macro%20basics/near/182209696" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BatmanAoD (Kyle Strand) <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Help.20with.20attribute.20macro.20basics.html#182209696">(Nov 29 2019 at 23:07)</a>:</h4>
<p>I'm still a bit surprised that <code>use flexible_bin_heap::with_comparator</code> works, while <code>use crate::with_comparator</code> doesn't, though.</p>



<a name="182209999"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Help%20with%20attribute%20macro%20basics/near/182209999" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Help.20with.20attribute.20macro.20basics.html#182209999">(Nov 29 2019 at 23:13)</a>:</h4>
<p>It normally uses def site hygiene, which means that it is like if you pasted the macro output in the file yourself, so names get resolved relative to the place you use the macro.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>